#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10, M = N * 2;
int n;
int h[N], e[M], ne[M], idx;
LL w[N];
LL f[N][3];
void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void dfs(int u, int fa)
{
    f[u][2] = w[u];

    int sum = 0;
    for (int i = h[u]; ~i; i = ne[i])
    {
        int j = e[i];
        if (fa == j)continue;
        dfs(j, u);
        f[u][0] += min(f[j][1], f[j][2]);
        f[u][2] += min(min(f[j][0], f[j][1]), f[j][2]);
        sum += min(f[j][1], f[j][2]);
    }

    f[u][1] = 1e15;
    for (int i = h[u]; ~i; i = ne[i])
    {
        int j = e[i];
        if (j == fa)continue;
        f[u][1] = min(f[u][1], sum - min(f[j][1], f[j][2]) + f[j][2]);
    }
}
 
int main()
{
    int t;
    cin >> t;
    while (t--) {
        scanf("%d", &n);
        idx = 0;
        memset(h, -1, sizeof h);
        for (int i = 1; i <= n; i++) {
            scanf("%lld", &w[i]);
            f[i][0] = 0;
            f[i][1] = 0;
            f[i][2] = 0;
        }
        if (n == 1) {
            printf("%lld\n", w[1]);
            continue;
        }
        for (int i = 0; i < n - 1; i++) {
            int a, b;
            scanf("%d%d", &a, &b);
            add(a, b);
            add(b, a);
        }
        dfs(1, 0);
        LL ans = min(f[1][1], f[1][2]);
        printf("%lld\n", ans);
    }

    return 0;
}

